package com.woniuxy.service.impl;

import com.woniuxy.daos.TbCustomerDao;
import com.woniuxy.daos.TbSendmessageDao;
import com.woniuxy.entitys.TbCustomer;
import com.woniuxy.entitys.TbSendmessage;
import com.woniuxy.service.RegisterService;
import org.apache.ibatis.annotations.Options;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author wu
 * @date 2024/01/16 18:15:53
 */
@Service
public class RegisterServiceImpl implements RegisterService {
    @Autowired
    private TbCustomerDao tbCustomerDao;
    @Autowired
    private TbSendmessageDao tbSendmessageDao;

    @Options(useGeneratedKeys=true, keyColumn="id")
    @Override
    public int register(TbCustomer tbCustomer) {
        //1.先看顾客表中是否有该账号
        //2.没有的情况下,则插入一条账号
        //3.注册成功,给消息表加一条数据
        String phone = tbCustomer.getPhone();
        TbCustomer customer = tbCustomerDao.selectByPhone(phone);
        if(customer==null){
            tbCustomer.setSoftdel(1);
            tbCustomer.setRegtime(new Date());
            int insert = tbCustomerDao.insert(tbCustomer);
            Integer cid = tbCustomer.getCid();
            TbSendmessage tbSendmessage = new TbSendmessage();
            tbSendmessage.setCid(cid);
            tbSendmessage.setMid(1);
            tbSendmessage.setSendTime(new Date());
            tbSendmessageDao.insert(tbSendmessage);
            return insert;
        }
        return 0;
    }
}
